/**
 * 试剂耗材详情
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/6
 */
var self = id = uid =  '';
var index = 1;

var files = [];  
document.addEventListener('plusready' , ready ,  false);

function ready(){
	self = plus.webview.currentWebview(); 
	var memberInfo = plus.storage.getItem('memberInfo');
	memberInfo = $.parseJSON(memberInfo);
	uid = memberInfo.aid;
	id = self.aid; 
	$('.name').text(self.name);
	$('.time').text(self.addtime);
	$('.details').text(self.details);
	//评价
	$('.add_evaluate').on('click' , function(){
		var status = self.status;
		if(status < 3){
			toast('还没有到评价的阶段哦');
			return;
		}
		plus.nativeUI.showWaiting('跳转中...');
		var me_order_appraise = plus.webview.create('me-order-appraise.html', 'me_order_appraise', {}, {
			uid : memberInfo.aid, 
			nickname : memberInfo.nickname,
			aid : id,
			businessid:self.business_id,
			title : self.name,
		}); 
		me_order_appraise.addEventListener('loaded', function (){
			me_order_appraise.show('pop-in');
			plus.nativeUI.closeWaiting();
		})	
	})
	
	//输入票号
	$('.scan').on('click' , function(){
		
		plus.nativeUI.actionSheet( {cancel:"取消",buttons:[{title:"拍照添加"},{title:"相册添加"}]}, function(e){
			
			if(e.index==1){
				
				var cmr = plus.camera.getCamera();
				
				cmr.captureImage(function(path) {
					
					appendPic(path);
					
				}, function(err) {});
				
			}else if(e.index==2){
				
				plus.gallery.pick(function(path){
					
					
					appendPic(path);
					
			    });
			    
			}
		})
		
		
//		plus.nativeUI.prompt('请输入票号' , function(e){
//			var number = e.value;
//			$.ajax({
//				type:"post",
//				url:apiRoot+"/Home/Orders/checkTicket/",
//				data:{
//					id : self.aid,
//					number : number
//				},
//				beforeSend:function(){
//					plus.nativeUI.showWaiting('提交中...');
//				},
//				complete:function(){
//					plus.nativeUI.closeWaiting();
//				},
//				success:function(data){
//					
//					if(data == 1){
//						toast('提交成功');
//					}else{
//						toast('失败 该票号已经提交过');
//					}
//				},
//				error:function(e){
//					toast('错误');
//				}
//			});
//			
//		})
	})
	
 	
}



// 添加照片
function appendPic(p) {  
	
	var newPath = p.replace(/\./g , new Date().getTime()+'.');
	
	plus.zip.compressImage({
		
			src:p,
			
			dst:newPath,
			
			quality:20
			
		},
		
		function() {
			
			files.push({name:"uploadkey"+index,path:p});
			
			index++;
			
			upload();
			
		},function(error) {
			
			toast('压缩图片失败');
			
	});
}

function upload(){
	
	plus.nativeUI.showWaiting('处理中...');
	
	if(files.length <= 0){  
		
		toast("请上传头像！");
		
		return;
		
	}
	
	var server = apiRoot+'/Home/Orders/uploadPic/id/'+self.aid; 
	
	var task = plus.uploader.createUpload(server,
		
		{method:"POST"},
		
		function(t,status){ //上传完成
			
			plus.nativeUI.closeWaiting();
			
			if(status == 200){ 
				
				toast('处理完成');
				
			}else{
				
				plus.nativeUI.alert("上传失败："+status);
				
				wt.close();
				
			}
		} 
	);
	
	for(var i = 0;i < files.length ; i++){
		
		var f = files[i];
		
		task.addFile(f.path,{key:f.name});
		
	} 
	
	task.start();
}
